package com.example.hello.mapper;

import com.example.hello.pojo.Book;
import com.example.hello.pojo.PageQueryParam;
import com.example.hello.pojo.PageResult;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface BookMapper {



    @Insert("insert into book (id,title, img, author, isbn, publisher, pubdate, pubplace, pages, price, binding, edition, format, summary, last_num, sum, recommend_num, create_time, update_time) " +
            "values (null,#{title}, #{img}, #{author}, #{isbn}, #{publisher}, #{pubdate}, #{pubplace}, #{pages}, #{price}, #{binding}, #{edition}, #{format}, #{summary}, #{lastNum}, #{sum}, #{recommendNum}, #{createTime}, #{updateTime});")
    void save(Book book);

    @Select("select * from book " +
            "where title like concat('%', #{requestParam}, '%') " +
            "or author like concat('%', #{requestParam}, '%') " +
            "or isbn like concat('%',#{requestParam}, '%')")
    List<Book> findByTitleOrAuthorOrISBN(String requestParam);

    @Select("select * from book " +
            "where title like concat('%', #{requestParam}, '%')" +
            "or author like concat('%', #{requestParam}, '%') " +
            "or isbn like concat('%',#{requestParam}, '%')" +
            " order by update_time desc")
    List<Book> findAll(PageQueryParam bookQueryParam);

    /**
     * 根据图书ID查询图书信息
     */
    @Select("SELECT * FROM book WHERE id = #{id}")
    Book getBookById(@Param("id") Integer id);

    /**
     * 更新图书剩余库存，将 lastNum 更新为指定值，同时更新更新时间
     */
    @Update("UPDATE book SET last_num = #{lastNum}, update_time = NOW() WHERE id = #{id}")
    int updateBookLastNum(@Param("id") Integer id, @Param("lastNum") Integer lastNum);

    /**
     * 更新图书剩余数量加 1
     *
     * @param bookId 图书ID
     * @return 受影响的记录数
     */
    @Update("UPDATE book SET last_num = last_num + 1, update_time = NOW() WHERE id = #{bookId}")
    void incrementBorrowNum(@Param("bookId") int bookId);

    /**
     * 更新图书借阅数量加 1
     *
     * @param bookId 图书ID
     * @return 受影响的记录数
     */
    @Update("UPDATE book SET recommend_num = recommend_num + 1, update_time = NOW() WHERE id = #{bookId}")
    int incrementLastNum(@Param("bookId") int bookId);


    /**
     * 根据id删除图书信息
     * @param id
     */
    @Delete("delete from book where id = #{id}")
    void deleteById(Integer id);

    /**
     * 更新图书信息
     * @param book
     */
    @Update("update book set title = #{title}, img = #{img}, author = #{author}, isbn = #{isbn}" +
            ", publisher = #{publisher}, pubdate = #{pubdate}, pubplace = #{pubplace}, pages = #{pages}, " +
            "price = #{price}, binding = #{binding}, edition = #{edition}, format = #{format}" +
            ", summary = #{summary}, last_num = #{lastNum}, sum = #{sum}, " +
            "update_time = #{updateTime} " +
            "where id = #{id}")
    void update(Book book);

    @Select("select id from book where isbn = #{bookIsbn}")
    Integer findBookIdByIsbn(String bookIsbn);

    @Select("select * from book " +
            "where title like concat('%', #{requestParam}, '%')" +
            "or author like concat('%', #{requestParam}, '%') " +
            "or isbn like concat('%',#{requestParam}, '%')" +
            " order by recommend_num desc")
    List<Book> findAllOrderByBorrowNum(PageQueryParam bookQueryParam);
}
